from tkinter import Toplevel
from tkinter import Label
from tkinter import Button
from tkinter import PhotoImage
from PIL import Image,ImageTk
import sys
import os
from text import INFO

def resource_path(relative_path):
    
    base_path = os.path.abspath(".")
    return os.path.join(base_path, relative_path)

class About(Toplevel):
    def __init__(self, parent, language):
        super().__init__()
        self.parent = parent # 显式地保留父窗口
        self.language = language
        self.window_init()
        self.display()
        if os.path.exists(resource_path(os.path.join("res", "synctech.ico"))):
            self.iconbitmap(resource_path(os.path.join("res", "synctech.ico")))
        self.protocol('WM_DELETE_WINDOW', self.on_window_closed)

    def window_init(self):
        """窗口属性初始化"""
        self.title(INFO['About'][self.language.get()])
        self.resizable(False,False)
        # 窗口宽高
        window_width = 300
        window_height = 220
        # 屏幕宽高
        screen_width = self.winfo_screenwidth()
        screen_height = self.winfo_screenheight()
        # 屏幕中心位置坐标
        pos_x = (screen_width - window_width) / 2
        pos_y = (screen_height - window_height) / 2
        # 设置屏幕宽、高、x位置、y位置
        self.geometry('%dx%d+%d+%d' % (window_width, window_height, pos_x, pos_y))

    def display(self):
        # self.img = Image.open('synctech.png')
        # self.photo = ImageTk.PhotoImage(self.img)
        if os.path.exists(resource_path(os.path.join("res","synctech.png"))):
            filename = resource_path(os.path.join("res","synctech.png"))
            self.photo = ImageTk.PhotoImage(Image.open(filename))

            # self.photo = ImageTk.PhotoImage(Image.open('res/synctech.png'))
            Label(self,image=self.photo).pack()
        l1 = Label(self,text=INFO['Company'][self.language.get()],font=('宋体',12),width=35,height=1).pack()
        l2 = Label(self,text="zhouyiheng@impcas.ac.cn",font=('Arial',12),width=25,height=1).pack()
        l3 = Label(self,text="V2.01",font=('Arial',12),width=15,height=1).pack()
        Button(self, text="OK", command=self.ok).pack(side='bottom',pady=10)

    def ok(self):
        self.parent.attributes("-disabled", 0)
        self.destroy()
    def on_window_closed(self):
        self.parent.attributes("-disabled", 0)
        self.destroy()
